﻿// ------------------------------------------------------------------------------
// <copyright file="AnimationHelper.cs" company="skumaronline.com">
//    Copyright (C) 2012 Suresh Kumar T.
//
//    This program is free software; you can redistribute it and/or
//    modify it under the terms of the GNU General Public License
//    as published by the Free Software Foundation version 2
//    of the License.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    http://www.gnu.org/licenses/gpl-2.0.html
//
// </copyright>
// ------------------------------------------------------------------------------

namespace StoreAppLib.Animations
{
    using System;
    using Windows.UI;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Media.Animation;

    /// <summary>
    /// Animation helper
    /// </summary>
    public static class AnimationHelper
    {
        /// <summary>
        /// Animates the value of a Color property between two target values using linear interpolation over a specified Duration. 
        /// </summary>
        /// <param name="element">The target element</param>
        /// <param name="property">The target property</param>
        /// <param name="toColor">Animation ending color</param>
        /// <param name="duration">Animation duration in milliseconds</param>
        public static void ColorAnimation(DependencyObject element, string property, Color toColor, double duration)
        {
            Storyboard storyboard = new Storyboard();

            ColorAnimation animation = new ColorAnimation();
            animation.To = toColor;
            animation.Duration = TimeSpan.FromMilliseconds(duration);

            Storyboard.SetTargetProperty(animation, property);

            storyboard.Children.Add(animation);

            Storyboard.SetTarget(animation, element);

            storyboard.Begin();
        }

        /// <summary>
        /// Animates the value of a Color property between two target values using linear interpolation over a specified Duration. 
        /// </summary>
        /// <param name="element">The target element</param>
        /// <param name="property">The target property</param>
        /// <param name="fromColor">Animation starting color</param>
        /// <param name="toColor">Animation ending color</param>
        /// <param name="duration">Animation duration in milliseconds</param>
        public static void ColorAnimation(DependencyObject element, string property, Color fromColor, Color toColor, double duration)
        {
            Storyboard storyboard = new Storyboard();

            ColorAnimation animation = new ColorAnimation();
            animation.From = fromColor;
            animation.To = toColor;
            animation.Duration = TimeSpan.FromMilliseconds(duration);

            Storyboard.SetTargetProperty(animation, property);

            storyboard.Children.Add(animation);

            Storyboard.SetTarget(animation, element);

            storyboard.Begin();
        }

        /// <summary>
        /// Animates the value of an object property along a single key frame for the specified duration
        /// </summary>
        /// <param name="element">The target element</param>
        /// <param name="property">The target property</param>
        /// <param name="value">Animation value</param>
        /// <param name="duration">Animation duration in milliseconds</param>
        public static void ObjectAnimationUsingKeyFrames(DependencyObject element, string property, object value, double duration)
        {
            Storyboard storyboard = new Storyboard();

            ObjectAnimationUsingKeyFrames animation = new ObjectAnimationUsingKeyFrames();

            DiscreteObjectKeyFrame keyFrame = new DiscreteObjectKeyFrame();
            keyFrame.Value = value;
            keyFrame.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(duration));
            animation.KeyFrames.Add(keyFrame);

            Storyboard.SetTargetProperty(animation, property);

            storyboard.Children.Add(animation);

            Storyboard.SetTarget(animation, element);

            storyboard.Begin();
        }

        /// <summary>
        /// Pointer down animation for a user action that taps an item or element
        /// </summary>
        /// <param name="element">The target element</param>
        public static void PointerDownThemeAnimation(DependencyObject element)
        {
            Storyboard storyboard = new Storyboard();

            PointerDownThemeAnimation animation = new PointerDownThemeAnimation();

            storyboard.Children.Add(animation);

            Storyboard.SetTarget(animation, element);

            storyboard.Begin();
        }

        /// <summary>
        /// Pointer up animation for a user action that taps an item or element
        /// </summary>
        /// <param name="element">The target element</param>
        public static void PointerUpThemeAnimation(DependencyObject element)
        {
            Storyboard storyboard = new Storyboard();

            PointerUpThemeAnimation animation = new PointerUpThemeAnimation();

            storyboard.Children.Add(animation);

            Storyboard.SetTarget(animation, element);

            storyboard.Begin();
        }

        /// <summary>
        /// Applies multiple transform operations to an object
        /// </summary>
        /// <param name="element">The target element</param>
        /// <param name="animateTo">Y coordinate</param>
        /// <param name="duration">Animation duration in milliseconds</param>
        public static void CompositeTransformTranslateY(UIElement element, double animateTo, double duration)
        {
            element.RenderTransform = new CompositeTransform();

            Storyboard storyboard = new Storyboard();

            DoubleAnimation animation = new DoubleAnimation();
            animation.Duration = TimeSpan.FromMilliseconds(duration);
            animation.To = animateTo;

            storyboard.Children.Add(animation);

            Storyboard.SetTarget(animation, element);
            Storyboard.SetTargetProperty(animation, "(UIElement.RenderTransform).(CompositeTransform.TranslateY)");

            storyboard.Begin();
        }

        //////public static void PopOutThemeAnimation(DependencyObject element)
        //////{
        //////    Storyboard storyboard = new Storyboard();

        //////    PopOutThemeAnimation animation = new PopOutThemeAnimation();

        //////    storyboard.Children.Add(animation);

        //////    Storyboard.SetTarget(animation, element);

        //////    //storyboard.Begin();
        //////}

        //////public static void PopInThemeAnimation(DependencyObject element)
        //////{
        //////    Storyboard storyboard = new Storyboard();

        //////    PopInThemeAnimation animation = new PopInThemeAnimation();

        //////    storyboard.Children.Add(animation);

        //////    Storyboard.SetTarget(animation, element);

        //////    //storyboard.Begin();
        //////}
    }
}